<?php
/**
 * 工具类
 * @author AlexLin
 *
 */
class Tools {
	/**
	 * 构造函数
	 */
	function __construct() {
	}
	/**
	 * 析构函数
	 */
	function __destruct() {
	}
	/**
	 * 获取当前时间
	 *
	 * @param string $format        	
	 * @return string
	 */
	public static function mb_GetCurDateTime($format = 'DATETIME') {
		$retData = '';
		switch ($format) {
			case 'DATETIME' :
				$retData = date ( 'Y-m-d H:i:s' );
				break;
			case 'DATE' :
				$retData = date ( 'Y-m-d' );
				break;
			case 'TIME' :
				$retData = date ( 'H:i:s' );
				break;
			default :
				$retData = date ( 'Y-m-d H:i:s' );
				break;
		}
		return $retData;
	}
	/**
	 * 随机
	 *
	 * @param number $len        	
	 * @param string $format        	
	 * @return string
	 */
	public static function mb_RandStr($len = 6, $format = 'CHARNUM') {
		switch ($format) {
			case 'ALL' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
				break;
			case 'CHAR' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
				break;
			case 'NUMBER' :
				$chars = '0123456789';
				break;
			case 'CHARNUM' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
				break;
			case 'LOWENG' :
				$chars = 'abcdefghijklmnopqrstuvwxyz';
				break;
			case 'UPENG' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
				break;
			case 'LOWNUM' :
				$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
				break;
			case 'UPNUM' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
				break;
			case 'DICE' :
				$chars = '123456';
				break;
			default :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
				break;
		}
		$password = "";
		while ( strlen ( $password ) < $len )
			$password .= substr ( $chars, (mt_rand () % strlen ( $chars )), 1 );
		return $password;
	}
	/**
	 * 获取时间差
	 *
	 * @param unknown $timeA
	 *        	较后时间
	 * @param unknown $timeB
	 *        	较前时间
	 * @return number
	 */
	public static function mb_GetTimeDifference($timeA, $timeB) { // 获取时间差
		$date = floor ( (strtotime ( $timeA ) - strtotime ( $timeB )) / 86400 );
		$hour = floor ( (strtotime ( $timeA ) - strtotime ( $timeB )) % 86400 / 3600 );
		$minute = floor ( (strtotime ( $timeA ) - strtotime ( $timeB )) % 86400 / 60 );
		$second = floor ( (strtotime ( $timeA ) - strtotime ( $timeB )) % 86400 % 60 );
		$timeDiff = $date * 24 * 60 * 60 + $hour * 60 * 60 + $minute * 60 + $second;
		return $timeDiff;
	}
	/**
	 * 写下日志
	 *
	 * @param string $word        	
	 */
	public static function mb_LogResult($word = '') {
		$logWord = '[' . date ( 'Y-m-d H:i:s' ) . '] ' . $word . "\r\n";
		$logPath = dirname ( __FILE__ ) . '\\Log\\';
		$logName = 'Log_' . date ( 'Y-m-d' ) . '.txt';
		$logFile = $logPath . $logName;
		if (! is_dir ( $logPath )) {
			mkdir ( $logPath );
		}
		$fp = null;
		if (! is_file ( $logFile )) {
			$fp = fopen ( $logFile, "w" );
		} else {
			$fp = fopen ( $logFile, "a" );
		}
		flock ( $fp, LOCK_EX );
		fwrite ( $fp, $logWord );
		flock ( $fp, LOCK_UN );
		fclose ( $fp );
	}
	/**
	 * http get 请求
	 *
	 * @param unknown $url        	
	 * @param array $param        	
	 * @throws Exception
	 * @return unknown
	 */
	public function mb_GetReq($url, $param = array()) {
		if (! is_array ( $param )) {
			throw new Exception ( "参数必须为array" );
		}
		$p = '';
		foreach ( $param as $key => $value ) {
			$p = $p . $key . '=' . $value . '&';
		}
		if (preg_match ( '/\?[\d\D]+/', $url )) { // matched ?c
			$p = '&' . $p;
		} else if (preg_match ( '/\?$/', $url )) { // matched ?$
			$p = $p;
		} else {
			$p = '?' . $p;
		}
		$p = preg_replace ( '/&$/', '', $p );
		$url = $url . $p;
		// echo $url;
		$httph = curl_init ( $url );
		curl_setopt ( $httph, CURLOPT_SSL_VERIFYPEER, 0 );
		curl_setopt ( $httph, CURLOPT_SSL_VERIFYHOST, 1 );
		curl_setopt ( $httph, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
		
		curl_setopt ( $httph, CURLOPT_RETURNTRANSFER, 1 );
		// curl_setopt ( $httph, CURLOPT_HEADER, 1 );
		$rst = curl_exec ( $httph );
		curl_close ( $httph );
		return $rst;
	}
	/**
	 * http post 请求
	 *
	 * @param unknown $url        	
	 * @param array $param        	
	 * @throws Exception
	 * @return unknown
	 */
	public function mb_PostReq($url, $param = array()) {
		// if (! is_array ( $param )) {
		// throw new Exception ( "参数必须为array" );
		// }
		$httph = curl_init ( $url );
		curl_setopt ( $httph, CURLOPT_SSL_VERIFYPEER, 0 );
		curl_setopt ( $httph, CURLOPT_SSL_VERIFYHOST, 1 );
		curl_setopt ( $httph, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
		curl_setopt ( $httph, CURLOPT_POST, 1 ); // 设置为POST方式
		curl_setopt ( $httph, CURLOPT_POSTFIELDS, $param );
		curl_setopt ( $httph, CURLOPT_RETURNTRANSFER, 1 );
		// curl_setopt ( $httph, CURLOPT_HEADER, 1 );
		$rst = curl_exec ( $httph );
		curl_close ( $httph );
		return $rst;
	}
	/**
	 * 过滤掉emoji表情
	 *
	 * @param string $str        	
	 * @return mixed
	 */
	public static function mb_FilterEmoji($str) {
		$str = preg_replace_callback ( '/./u', function (array $match) {
			return strlen ( $match [0] ) >= 4 ? '' : $match [0];
		}, $str );
		
		return $str;
	}
	/**
	 * 根据两点间的经纬度计算距离
	 *
	 * @param float $lat
	 *        	纬度值
	 * @param float $lng
	 *        	经度值
	 */
	public static function mb_GetDistance($lat1, $lng1, $lat2, $lng2) {
		$earthRadius = 6367000; // approximate radius of earth in meters
		$lat1 = ($lat1 * pi ()) / 180;
		$lng1 = ($lng1 * pi ()) / 180;
		$lat2 = ($lat2 * pi ()) / 180;
		$lng2 = ($lng2 * pi ()) / 180;
		$calcLongitude = $lng2 - $lng1;
		$calcLatitude = $lat2 - $lat1;
		$stepOne = pow ( sin ( $calcLatitude / 2 ), 2 ) + cos ( $lat1 ) * cos ( $lat2 ) * pow ( sin ( $calcLongitude / 2 ), 2 );
		$stepTwo = 2 * asin ( min ( 1, sqrt ( $stepOne ) ) );
		$calculatedDistance = $earthRadius * $stepTwo;
		return round ( $calculatedDistance );
	}
}

/**
 * 微信支付服务器端下单
 * 微信APP支付文档地址: https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_6
 * 使用示例
 * 构造方法参数
 * 'appid' => //填写微信分配的公众账号ID
 * 'mch_id' => //填写微信支付分配的商户号
 * 'notify_url'=> //填写微信支付结果回调地址
 * 'key' => //填写微信商户支付密钥
 * );
 * 统一下单方法
 * $WechatAppPay = new wechatAppPay($options);
 * $params['body'] = '商品描述'; //商品描述
 * $params['out_trade_no'] = '1217752501201407'; //自定义的订单号，不能重复
 * $params['total_fee'] = '100'; //订单金额 只能为整数 单位为分
 * $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE |APP | WAP
 * $wechatAppPay->unifiedOrder( $params );
 */
class wechatAppPay {
	// 接口API URL前缀
	const API_URL_PREFIX = 'https://api.mch.weixin.qq.com';
	// 下单地址URL
	const UNIFIEDORDER_URL = "/pay/unifiedorder";
	// 查询订单URL
	const ORDERQUERY_URL = "/pay/orderquery";
	// 关闭订单URL
	const CLOSEORDER_URL = "/pay/closeorder";
	// 公众账号ID
	private $appid;
	// 商户号
	private $mch_id;
	// 随机字符串
	private $nonce_str;
	// 签名
	private $sign;
	// 商品描述
	private $body;
	// 商户订单号
	private $out_trade_no;
	// 支付总金额
	private $total_fee;
	// 终端IP
	private $spbill_create_ip;
	// 支付结果回调通知地址
	private $notify_url;
	// 交易类型
	private $trade_type;
	// 支付密钥
	private $key;
	// 证书路径
	private $SSLCERT_PATH;
	private $SSLKEY_PATH;
	// 所有参数
	private $params = array ();
	public function __construct($appid, $mch_id, $notify_url, $key) {
		$this->appid = $appid;
		$this->mch_id = $mch_id;
		$this->notify_url = $notify_url;
		$this->key = $key;
	}
	/**
	 * 下单方法
	 *
	 * @param $params 下单参数        	
	 */
	public function unifiedOrder($params) {
		$this->body = $params ['body'];
		$this->out_trade_no = $params ['out_trade_no'];
		$this->total_fee = $params ['total_fee'];
		$this->trade_type = $params ['trade_type'];
		$this->nonce_str = $this->genRandomString ();
		$this->spbill_create_ip = $_SERVER ['REMOTE_ADDR'];
		$this->params ['appid'] = $this->appid;
		$this->params ['attach'] = $params ['attach'];
		$this->params ['mch_id'] = $this->mch_id;
		$this->params ['nonce_str'] = $this->nonce_str;
		$this->params ['body'] = $this->body;
		$this->params ['out_trade_no'] = $this->out_trade_no;
		$this->params ['total_fee'] = $this->total_fee;
		$this->params ['spbill_create_ip'] = $this->spbill_create_ip;
		$this->params ['notify_url'] = $this->notify_url;
		$this->params ['trade_type'] = $this->trade_type;
		// 获取签名数据
		$this->sign = $this->MakeSign ( $this->params );
		$this->params ['sign'] = $this->sign;
		$xml = $this->data_to_xml ( $this->params );
		$response = $this->postXmlCurl ( $xml, self::API_URL_PREFIX . self::UNIFIEDORDER_URL );
		if (! $response) {
			return false;
		}
		$result = $this->xml_to_data ( $response );
		if (! empty ( $result ['result_code'] ) && ! empty ( $result ['err_code'] )) {
			$result ['err_msg'] = $this->error_code ( $result ['err_code'] );
		}
		return $result;
	}
	/**
	 * 查询订单信息
	 *
	 * @param $out_trade_no 订单号        	
	 * @return array
	 */
	public function orderQuery($out_trade_no) {
		$this->params ['appid'] = $this->appid;
		$this->params ['mch_id'] = $this->mch_id;
		$this->params ['nonce_str'] = $this->genRandomString ();
		$this->params ['out_trade_no'] = $out_trade_no;
		// 获取签名数据
		$this->sign = $this->MakeSign ( $this->params );
		$this->params ['sign'] = $this->sign;
		$xml = $this->data_to_xml ( $this->params );
		$response = $this->postXmlCurl ( $xml, self::API_URL_PREFIX . self::ORDERQUERY_URL );
		if (! $response) {
			return false;
		}
		$result = $this->xml_to_data ( $response );
		if (! empty ( $result ['result_code'] ) && ! empty ( $result ['err_code'] )) {
			$result ['err_msg'] = $this->error_code ( $result ['err_code'] );
		}
		return $result;
	}
	/**
	 * 关闭订单
	 *
	 * @param $out_trade_no 订单号        	
	 * @return array
	 */
	public function closeOrder($out_trade_no) {
		$this->params ['appid'] = $this->appid;
		$this->params ['mch_id'] = $this->mch_id;
		$this->params ['nonce_str'] = $this->genRandomString ();
		$this->params ['out_trade_no'] = $out_trade_no;
		// 获取签名数据
		$this->sign = $this->MakeSign ( $this->params );
		$this->params ['sign'] = $this->sign;
		$xml = $this->data_to_xml ( $this->params );
		$response = $this->postXmlCurl ( $xml, self::API_URL_PREFIX . self::CLOSEORDER_URL );
		if (! $response) {
			return false;
		}
		$result = $this->xml_to_data ( $response );
		return $result;
	}
	/**
	 * 获取支付结果通知数据
	 * return array
	 */
	public function getNotifyData() {
		// 获取通知的数据
		$xml = $GLOBALS ['HTTP_RAW_POST_DATA'];
		$data = array ();
		if (empty ( $xml )) {
			return false;
		}
		$data = $this->xml_to_data ( $xml );
		if (! empty ( $data ['return_code'] )) {
			if ($data ['return_code'] == 'FAIL') {
				return false;
			}
		}
		return $data;
	}
	/**
	 * 接收通知成功后应答输出XML数据
	 *
	 * @param string $xml        	
	 */
	public function replyNotify() {
		$data ['return_code'] = 'SUCCESS';
		$data ['return_msg'] = 'OK';
		$xml = $this->data_to_xml ( $data );
		echo $xml;
		die ();
	}
	/**
	 * 生成APP端支付参数
	 *
	 * @param $prepayid 预支付id        	
	 */
	public function getAppPayParams($prepayid) {
		$data ['appid'] = $this->appid;
		$data ['partnerid'] = $this->mch_id;
		$data ['prepayid'] = $prepayid;
		$data ['package'] = 'Sign=WXPay';
		$data ['noncestr'] = $this->genRandomString ();
		$data ['timestamp'] = time ();
		$data ['sign'] = $this->MakeSign ( $data );
		return $data;
	}
	/**
	 * 生成签名
	 *
	 * @return 签名
	 */
	public function MakeSign($params) {
		// 签名步骤一：按字典序排序数组参数
		ksort ( $params );
		$string = $this->ToUrlParams ( $params );
		// 签名步骤二：在string后加入KEY
		$string = $string . "&key=" . $this->key;
		// 签名步骤三：MD5加密
		$string = md5 ( $string );
		// 签名步骤四：所有字符转为大写
		$result = strtoupper ( $string );
		return $result;
	}
	/**
	 * 将参数拼接为url: key=value&key=value
	 *
	 * @param
	 *        	$params
	 * @return string
	 */
	public function ToUrlParams($params) {
		$string = '';
		if (! empty ( $params )) {
			$array = array ();
			foreach ( $params as $key => $value ) {
				$array [] = $key . '=' . $value;
			}
			$string = implode ( "&", $array );
		}
		return $string;
	}
	/**
	 * 输出xml字符
	 *
	 * @param $params 参数名称
	 *        	return string 返回组装的xml
	 *        	
	 */
	public function data_to_xml($params) {
		if (! is_array ( $params ) || count ( $params ) <= 0) {
			return false;
		}
		$xml = "<xml>";
		foreach ( $params as $key => $val ) {
			if (is_numeric ( $val )) {
				$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
			} else {
				$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
			}
		}
		$xml .= "</xml>";
		return $xml;
	}
	/**
	 * 将xml转为array
	 *
	 * @param string $xml
	 *        	return array
	 */
	public function xml_to_data($xml) {
		if (! $xml) {
			return false;
		}
		// 将XML转为array
		// 禁止引用外部xml实体
		libxml_disable_entity_loader ( true );
		$data = json_decode ( json_encode ( simplexml_load_string ( $xml, 'SimpleXMLElement', LIBXML_NOCDATA ) ), true );
		return $data;
	}
	/**
	 * 获取毫秒级别的时间戳
	 */
	private static function getMillisecond() {
		// 获取毫秒的时间戳
		$time = explode ( " ", microtime () );
		$time = $time [1] . ($time [0] * 1000);
		$time2 = explode ( ".", $time );
		$time = $time2 [0];
		return $time;
	}
	/**
	 * 产生一个指定长度的随机字符串,并返回给用户
	 *
	 * @param type $len
	 *        	产生字符串的长度
	 * @return string 随机字符串
	 */
	private function genRandomString($len = 32) {
		$chars = array (
				"a",
				"b",
				"c",
				"d",
				"e",
				"f",
				"g",
				"h",
				"i",
				"j",
				"k",
				"l",
				"m",
				"n",
				"o",
				"p",
				"q",
				"r",
				"s",
				"t",
				"u",
				"v",
				"w",
				"x",
				"y",
				"z",
				"A",
				"B",
				"C",
				"D",
				"E",
				"F",
				"G",
				"H",
				"I",
				"J",
				"K",
				"L",
				"M",
				"N",
				"O",
				"P",
				"Q",
				"R",
				"S",
				"T",
				"U",
				"V",
				"W",
				"X",
				"Y",
				"Z",
				"0",
				"1",
				"2",
				"3",
				"4",
				"5",
				"6",
				"7",
				"8",
				"9" 
		);
		$charsLen = count ( $chars ) - 1;
		// 将数组打乱
		shuffle ( $chars );
		$output = "";
		for($i = 0; $i < $len; $i ++) {
			$output .= $chars [mt_rand ( 0, $charsLen )];
		}
		return $output;
	}
	/**
	 * 以post方式提交xml到对应的接口url
	 *
	 * @param string $xml
	 *        	需要post的xml数据
	 * @param string $url
	 *        	url
	 * @param bool $useCert
	 *        	是否需要证书，默认不需要
	 * @param int $second
	 *        	url执行超时时间，默认30s
	 * @throws WxPayException
	 */
	private function postXmlCurl($xml, $url, $useCert = false, $second = 30) {
		$ch = curl_init ();
		// 设置超时
		curl_setopt ( $ch, CURLOPT_TIMEOUT, $second );
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
		// 设置header
		curl_setopt ( $ch, CURLOPT_HEADER, FALSE );
		// 要求结果为字符串且输出到屏幕上
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
		if ($useCert == true) {
			// 设置证书
			// 使用证书：cert 与 key 分别属于两个.pem文件
			curl_setopt ( $ch, CURLOPT_SSLCERTTYPE, 'PEM' );
			// curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH);
			curl_setopt ( $ch, CURLOPT_SSLKEYTYPE, 'PEM' );
			// curl_setopt($ch,CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH);
		}
		// post提交方式
		curl_setopt ( $ch, CURLOPT_POST, TRUE );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $xml );
		// 运行curl
		$data = curl_exec ( $ch );
		// 返回结果
		if ($data) {
			curl_close ( $ch );
			return $data;
		} else {
			$error = curl_errno ( $ch );
			curl_close ( $ch );
			return false;
		}
	}
	/**
	 * 错误代码
	 *
	 * @param $code 服务器输出的错误代码
	 *        	return string
	 */
	public function error_code($code) {
		$errList = array (
				'NOAUTH' => '商户未开通此接口权限',
				'NOTENOUGH' => '用户帐号余额不足',
				'ORDERNOTEXIST' => '订单号不存在',
				'ORDERPAID' => '商户订单已支付，无需重复操作',
				'ORDERCLOSED' => '当前订单已关闭，无法支付',
				'SYSTEMERROR' => '系统错误!系统超时',
				'APPID_NOT_EXIST' => '参数中缺少APPID',
				'MCHID_NOT_EXIST' => '参数中缺少MCHID',
				'APPID_MCHID_NOT_MATCH' => 'appid和mch_id不匹配',
				'LACK_PARAMS' => '缺少必要的请求参数',
				'OUT_TRADE_NO_USED' => '同一笔交易不能多次提交',
				'SIGNERROR' => '参数签名结果不正确',
				'XML_FORMAT_ERROR' => 'XML格式错误',
				'REQUIRE_POST_METHOD' => '未使用post传递参数 ',
				'POST_DATA_EMPTY' => 'post数据不能为空',
				'NOT_UTF8' => '未使用指定编码格式' 
		);
		if (array_key_exists ( $code, $errList )) {
			return $errList [$code];
		}
	}
}

/**
 * 高德地图助手
 *
 * @author AlexLin
 *        
 */
class AmapHandler {
	/**
	 *
	 * @var unknown
	 */
	private $m_Key;
	/**
	 * web服务器请求连接
	 *
	 * @var string
	 */
	private $m_Url = 'https://restapi.amap.com/v3/';
	/**
	 * 构造函数
	 *
	 * @param string $key        	
	 */
	public function __construct($key) {
		$this->m_Key = $key;
	}
	/**
	 * 析构函数
	 */
	public function __destruct() {
	}
	/**
	 * IP定位
	 *
	 * @param string $ip        	
	 */
	public function mb_IpLocation($ip) {
		$url = $this->m_Url . 'ip';
		return json_decode ( Tools::mb_GetReq ( $url, array (
				'key' => $this->m_Key,
				'ip' => $ip,
				'output' => 'JSON' 
		) ) );
	}
}
class WMHandler {
	/**
	 * 单例
	 *
	 * @var WMHandler
	 */
	private static $m_Instance;
	/**
	 *
	 * @var string
	 */
	private $m_Url = 'http://127.0.0.1:1007';
	private $m_Param = array ();
	/**
	 * 构造函数
	 */
	private function __construct() {
		$this->m_Param ['zone'] = 'Web';
	}
	/**
	 * 复制方法
	 */
	private function __clone() {
	}
	/**
	 * 获取单例
	 *
	 * @return WMHandler
	 */
	public static function mb_GetInstance() {
		if (! (self::$m_Instance instanceof self)) {
			self::$m_Instance = new self ();
		}
		return self::$m_Instance;
	}
	/**
	 * 更新用户数据
	 *
	 * @param unknown $name        	
	 */
	public function mb_UpdateUserData($name) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_UpdateUserData',
				'name' => $name 
		
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	/**
	 * 更新大厅公告，滑动公告，充值公告信息
	 *
	 * @param unknown $key        	
	 * @param unknown $value        	
	 */
	public function mb_UpdateLobbyMessage($key, $value) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_UpdateLobbyMessages',
				'key' => $key,
				'value' => $value 
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	/**
	 * 更新大厅信息
	 */
	public function mb_UpdateLobbyInfo($status, $regist, $login, $phone, $bulletin, $broadcast, $recharge) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_UpdateLobbInfo',
				'status' => $status,
				'sw_regist' => $regist,
				'sw_login' => $login,
				'sw_phone' => $phone,
				'bulletin' => $bulletin,
				'broadcast' => $broadcast,
				'recharge' => $recharge 
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	public function mb_UpdateGameStatus($gameID, $status) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_ChangeAppStatus',
				'appid' => $gameID,
				'status' => $status 
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	public function mb_UpdateGameCoinValue($gameID, $coinValue) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_ChangeAppCoinValue',
				'appid' => $gameID,
				'coinvalue' => $coinValue 
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	public function mb_UpdateGameLevel($gameID, $level) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_ChangeAppLevel',
				'appid' => $gameID,
				'level' => $level 
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	/**
	 *
	 * @return string
	 */
	public function mb_GetOnlineUserIds() {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_GetOnlineUsers' 
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	/**
	 * 强制下线
	 *
	 * @param unknown $name        	
	 * @return mixed
	 */
	public function mb_ForcedOffline($name) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_ForcedOffline',
				'name' => $name 
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	/**
	 *
	 * @param unknown $name        	
	 * @param unknown $newPass        	
	 * @return mixed
	 */
	public function mb_UpdateUserPass($name, $pass) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_UpdateUserPass',
				'name' => $name,
				'pass' => $pass 
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	/**
	 * 
	 * @param unknown $name
	 * @param unknown $pass
	 * @return mixed
	 */
	public function mb_GetUserGameStatus($name) {
		$params = array (
				'zone' => 'Web',
				'method' => 'mb_GetUserGameStatus',
				'name' => $name,
		);
		return $this->mb_SendGet ( $this->m_Url, $params );
	}
	/**
	 * 发送POST数据
	 *
	 * @param unknown $url        	
	 * @param unknown $pData        	
	 * @return string
	 */
	private function mb_SendPost($url, $pData) {
		$postdata = http_build_query ( $pData );
		$options = array (
				'http' => array (
						'method' => 'POST',
						'header' => 'Content-type:application/x-www-form-urlencoded',
						'content' => $postdata,
						'timeout' => 15 * 60  // 超时时间（单位:s）
				) 
		);
		$context = stream_context_create ( $options );
		$result = file_get_contents ( $url, false, $context );
		return $result;
	}
	/**
	 * 发送GET数据
	 *
	 * @param unknown $url        	
	 * @param unknown $gData        	
	 * @return mixed
	 */
	private function mb_SendGet($url, $gData) {
		$query = http_build_query ( $gData );
		$curl = curl_init ();
		// 设置抓取的url
		curl_setopt ( $curl, CURLOPT_URL, $url . '?' . $query );
		// 设置头文件的信息作为数据流输出
		curl_setopt ( $curl, CURLOPT_HEADER, false );
		// 设置获取的信息以文件流的形式返回，而不是直接输出。
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
		// 执行命令
		$data = curl_exec ( $curl );
		// curl_getinfo($curl,CURLINFO_HTTP_CODE); //输出请求状态码
		// 关闭URL请求
		curl_close ( $curl );
		// 显示获得的数据
		return $data;
	}
}
class RepData {
	/**
	 * 回复结果
	 * @var unknown
	 */
	public $m_Result;
	/**
	 * 回复数据
	 * @var unknown
	 */
	public $m_Data;
	/**
	 * 数据校验位
	 * @var unknown
	 */
	public $m_Sign;
}